# Configure paths for GiNaC
# Richard Kreckel 12/12/2000
# borrowed from Christian Bauer
# stolen from Sam Lantinga
# stolen from Manish Singh
# stolen back from Frank Belew
# stolen from Manish Singh
# Shamelessly stolen from Owen Taylor

dnl AM_PATH_GINAC([MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]])
dnl Test for GiNaC, and define GINACLIB_CPPFLAGS and GINACLIB_LIBS
dnl
AC_DEFUN([AM_PATH_GINAC],
[dnl 
dnl Get the cppflags and libraries from the ginac-config script
dnl
AC_ARG_WITH(ginac-prefix,[  --with-ginac-prefix=PFX Prefix where GiNaC is installed (optional)],
            ginac_prefix="$withval", ginac_prefix="")
AC_ARG_WITH(ginac-exec-prefix,[  --with-ginac-exec-prefix=PFX Exec prefix where GiNaC is installed (optional)],
            ginac_exec_prefix="$withval", ginac_exec_prefix="")
AC_ARG_ENABLE(ginactest, [  --disable-ginactest     Do not try to compile and run a test GiNaC program],
              , enable_ginactest=yes)

if test x$ginac_exec_prefix != x ; then
    ginac_args="$ginac_args --exec-prefix=$ginac_exec_prefix"
    if test x${GINACLIB_CONFIG+set} != xset ; then
        GINACLIB_CONFIG=$ginac_exec_prefix/bin/ginac-config
    fi
fi
if test x$ginac_prefix != x ; then
    ginac_args="$ginac_args --prefix=$ginac_prefix"
    if test x${GINACLIB_CONFIG+set} != xset ; then
        GINACLIB_CONFIG=$ginac_prefix/bin/ginac-config
    fi
fi

AC_PATH_PROG(GINACLIB_CONFIG, ginac-config, no)
ginac_min_version=ifelse([$1], ,0.7.0,$1)
AC_MSG_CHECKING(for GiNaC - version >= $ginac_min_version)
if test "$GINACLIB_CONFIG" = "no" ; then
    AC_MSG_RESULT(no)
    echo "*** The ginac-config script installed by GiNaC could not be found"
    echo "*** If GiNaC was installed in PREFIX, make sure PREFIX/bin is in"
    echo "*** your path, or set the GINACLIB_CONFIG environment variable to the"
    echo "*** full path to ginac-config."
    ifelse([$3], , :, [$3])
else
dnl Parse required version and the result of ginac-config.
    ginac_min_major_version=`echo $ginac_min_version | \
            sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
    ginac_min_minor_version=`echo $ginac_min_version | \
            sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
    ginac_min_micro_version=`echo $ginac_min_version | \
            sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
    GINACLIB_CPPFLAGS=`$GINACLIB_CONFIG $ginac_args --cppflags`
    GINACLIB_LIBS=`$GINACLIB_CONFIG $ginac_args --libs`
    ginac_config_version=`$GINACLIB_CONFIG $ginac_args --version`
    ginac_config_major_version=`echo $ginac_config_version | \
            sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
    ginac_config_minor_version=`echo $ginac_config_version | \
            sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
    ginac_config_micro_version=`echo $ginac_config_version | \
            sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
dnl Check if the installed GiNaC is sufficiently new according to ginac-config.
    if test \( $ginac_config_major_version -lt $ginac_min_major_version \) -o \
            \( $ginac_config_major_version -eq $ginac_min_major_version -a $ginac_config_minor_version -lt $ginac_min_minor_version \) -o \
            \( $ginac_config_major_version -eq $ginac_min_major_version -a $ginac_config_minor_version -eq $ginac_min_minor_version -a $ginac_config_micro_version -lt $ginac_min_micro_version \); then
        echo -e "\n*** 'ginac-config --version' returned $ginac_config_major_version.$ginac_config_minor_version.$ginac_config_micro_version, but the minimum version"
        echo "*** of GiNaC required is $ginac_min_major_version.$ginac_min_minor_version.$ginac_min_micro_version. If ginac-config is correct, then it is"
        echo "*** best to upgrade to the required version."
        echo "*** If ginac-config was wrong, set the environment variable GINACLIB_CONFIG"
        echo "*** to point to the correct copy of ginac-config, and remove the file"
        echo "*** config.cache before re-running configure."
        ifelse([$3], , :, [$3])
    else
dnl The versions match so far.  Now do a sanity check: Does the result of ginac-config
dnl match the version of the headers and the version built into the library, too?
        no_ginac=""
        if test "x$enable_ginactest" = "xyes" ; then
            ac_save_CPPFLAGS="$CPPFLAGS"
            ac_save_LIBS="$LIBS"
            CPPFLAGS="$CPPFLAGS $GINACLIB_CPPFLAGS"
            LIBS="$LIBS $GINACLIB_LIBS"
            rm -f conf.ginactest
            AC_TRY_RUN([
#include <stdio.h>
#include <string.h>
#include <ginac/version.h>

/* we do not #include <stdlib.h> because autoconf in C++ mode inserts a
   prototype for exit() that conflicts with the one in stdlib.h */
extern "C" int system(const char *);

int main(void)
{
    int major, minor, micro;
    char *tmp_version;

    system("touch conf.ginactest");

    if ((GINACLIB_MAJOR_VERSION != $ginac_config_major_version) ||
        (GINACLIB_MINOR_VERSION != $ginac_config_minor_version) ||
        (GINACLIB_MICRO_VERSION != $ginac_config_micro_version)) {
        printf("\n*** 'ginac-config --version' returned %d.%d.%d, but the header file I found\n", $ginac_config_major_version, $ginac_config_minor_version, $ginac_config_micro_version);
        printf("*** corresponds to %d.%d.%d. This mismatch suggests your installation of GiNaC\n", GINACLIB_MAJOR_VERSION, GINACLIB_MINOR_VERSION, GINACLIB_MICRO_VERSION);
        printf("*** is corrupted or you have specified some wrong -I compiler flags.\n");
        printf("*** Please inquire and consider reinstalling GiNaC.\n");
        return 1;
    }
    if ((GiNaC::version_major != $ginac_config_major_version) ||
        (GiNaC::version_minor != $ginac_config_minor_version) ||
        (GiNaC::version_micro != $ginac_config_micro_version)) {
        printf("\n*** 'ginac-config --version' returned %d.%d.%d, but the library I found\n", $ginac_config_major_version, $ginac_config_minor_version, $ginac_config_micro_version);
        printf("*** corresponds to %d.%d.%d. This mismatch suggests your installation of GiNaC\n", GiNaC::version_major, GiNaC::version_minor, GiNaC::version_micro);
        printf("*** is corrupted or you have specified some wrong -L compiler flags.\n");
        printf("*** Please inquire and consider reinstalling GiNaC.\n");
        return 1;
    }
    return 0;
}
],, no_ginac=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
            CPPFLAGS="$ac_save_CPPFLAGS"
            LIBS="$ac_save_LIBS"
        fi
        if test "x$no_ginac" = x ; then
            AC_MSG_RESULT([yes, $ginac_config_version])
            ifelse([$2], , :, [$2])
        else
            AC_MSG_RESULT(no)
            if test ! -f conf.ginactest ; then
                echo "*** Could not run GiNaC test program, checking why..."
                CPPFLAGS="$CFLAGS $GINACLIB_CPPFLAGS"
                LIBS="$LIBS $GINACLIB_LIBS"
                AC_TRY_LINK([
#include <stdio.h>
#include <ginac/version.h>
],              [ return 0; ],
                [ echo "*** The test program compiled, but did not run. This usually means"
                  echo "*** that the run-time linker is not finding GiNaC or finding the wrong"
                  echo "*** version of GiNaC. If it is not finding GiNaC, you'll need to set your"
                  echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
                  echo "*** to the installed location. Also, make sure you have run ldconfig if that"
                  echo "*** is required on your system."],
                [ echo "*** The test program failed to compile or link. See the file config.log for the"
                  echo "*** exact error that occured. This usually means GiNaC was incorrectly installed"
                  echo "*** or that you have moved GiNaC since it was installed. In the latter case, you"
                  echo "*** may want to edit the ginac-config script: $GINACLIB_CONFIG." ])
                CPPFLAGS="$ac_save_CPPFLAGS"
                LIBS="$ac_save_LIBS"
            fi
            GINACLIB_CPPFLAGS=""
            GINACLIB_LIBS=""
            ifelse([$3], , :, [$3])
        fi
    fi
fi
AC_SUBST(GINACLIB_CPPFLAGS)
AC_SUBST(GINACLIB_LIBS)
rm -f conf.ginactest
])

# Configure path for the GNU Scientific Library
# Christopher R. Gabriel <cgabriel@linux.it>, April 2000


AC_DEFUN([AM_PATH_GSL],
[
AC_ARG_WITH(gsl-prefix,[  --with-gsl-prefix=PFX   Prefix where GSL is installed (optional)],
            gsl_prefix="$withval", gsl_prefix="")
AC_ARG_WITH(gsl-exec-prefix,[  --with-gsl-exec-prefix=PFX Exec prefix where GSL is installed (optional)],
            gsl_exec_prefix="$withval", gsl_exec_prefix="")
AC_ARG_ENABLE(gsltest, [  --disable-gsltest       Do not try to compile and run a test GSL program],
		    , enable_gsltest=yes)

  if test "x${GSL_CONFIG+set}" != xset ; then
     if test "x$gsl_prefix" != x ; then
         GSL_CONFIG="$gsl_prefix/bin/gsl-config"
     fi
     if test "x$gsl_exec_prefix" != x ; then
        GSL_CONFIG="$gsl_exec_prefix/bin/gsl-config"
     fi
  fi

  AC_PATH_PROG(GSL_CONFIG, gsl-config, no)
  min_gsl_version=ifelse([$1], ,0.2.5,$1)
  AC_MSG_CHECKING(for GSL - version >= $min_gsl_version)
  no_gsl=""
  if test "$GSL_CONFIG" = "no" ; then
    no_gsl=yes
  else
    GSL_CFLAGS=`$GSL_CONFIG --cflags`
    GSL_LIBS=`$GSL_CONFIG --libs`

    gsl_major_version=`$GSL_CONFIG --version | \
           sed 's/^\([[0-9]]*\).*/\1/'`
    if test "x${gsl_major_version}" = "x" ; then
       gsl_major_version=0
    fi

    gsl_minor_version=`$GSL_CONFIG --version | \
           sed 's/^\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\).*/\2/'`
    if test "x${gsl_minor_version}" = "x" ; then
       gsl_minor_version=0
    fi

    gsl_micro_version=`$GSL_CONFIG --version | \
           sed 's/^\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\).*/\3/'`
    if test "x${gsl_micro_version}" = "x" ; then
       gsl_micro_version=0
    fi

    if test "x$enable_gsltest" = "xyes" ; then
      ac_save_CFLAGS="$CFLAGS"
      ac_save_LIBS="$LIBS"
      CFLAGS="$CFLAGS $GSL_CFLAGS"
      LIBS="$LIBS $GSL_LIBS"

      rm -f conf.gsltest
      AC_TRY_RUN([
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* my_strdup (const char *str);

char*
my_strdup (const char *str)
{
  char *new_str;
  
  if (str)
    {
      new_str = (char *)malloc ((strlen (str) + 1) * sizeof(char));
      strcpy (new_str, str);
    }
  else
    new_str = NULL;
  
  return new_str;
}

int main (void)
{
  int major = 0, minor = 0, micro = 0;
  int n;
  char *tmp_version;

  system ("touch conf.gsltest");

  /* HP/UX 9 (%@#!) writes to sscanf strings */
  tmp_version = my_strdup("$min_gsl_version");

  n = sscanf(tmp_version, "%d.%d.%d", &major, &minor, &micro) ;

  if (n != 2 && n != 3) {
     printf("%s, bad version string\n", "$min_gsl_version");
     exit(1);
   }

   if (($gsl_major_version > major) ||
      (($gsl_major_version == major) && ($gsl_minor_version > minor)) ||
      (($gsl_major_version == major) && ($gsl_minor_version == minor) && ($gsl_micro_version >= micro)))
    {
      exit(0);
    }
  else
    {
      printf("\n*** 'gsl-config --version' returned %d.%d.%d, but the minimum version\n", $gsl_major_version, $gsl_minor_version, $gsl_micro_version);
      printf("*** of GSL required is %d.%d.%d. If gsl-config is correct, then it is\n", major, minor, micro);
      printf("*** best to upgrade to the required version.\n");
      printf("*** If gsl-config was wrong, set the environment variable GSL_CONFIG\n");
      printf("*** to point to the correct copy of gsl-config, and remove the file\n");
      printf("*** config.cache before re-running configure\n");
      exit(1);
    }
}

],, no_gsl=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
       CFLAGS="$ac_save_CFLAGS"
       LIBS="$ac_save_LIBS"
     fi
  fi
  if test "x$no_gsl" = x ; then
     AC_MSG_RESULT(yes)
     ifelse([$2], , :, [$2])     
  else
     AC_MSG_RESULT(no)
     if test "$GSL_CONFIG" = "no" ; then
       echo "*** The gsl-config script installed by GSL could not be found"
       echo "*** If GSL was installed in PREFIX, make sure PREFIX/bin is in"
       echo "*** your path, or set the GSL_CONFIG environment variable to the"
       echo "*** full path to gsl-config."
     else
       if test -f conf.gsltest ; then
        :
       else
          echo "*** Could not run GSL test program, checking why..."
          CFLAGS="$CFLAGS $GSL_CFLAGS"
          LIBS="$LIBS $GSL_LIBS"
          AC_TRY_LINK([
#include <stdio.h>
],      [ return 0; ],
        [ echo "*** The test program compiled, but did not run. This usually means"
          echo "*** that the run-time linker is not finding GSL or finding the wrong"
          echo "*** version of GSL. If it is not finding GSL, you'll need to set your"
          echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
          echo "*** to the installed location  Also, make sure you have run ldconfig if that"
          echo "*** is required on your system"
	  echo "***"
          echo "*** If you have an old version installed, it is best to remove it, although"
          echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH"],
        [ echo "*** The test program failed to compile or link. See the file config.log for the"
          echo "*** exact error that occured. This usually means GSL was incorrectly installed"
          echo "*** or that you have moved GSL since it was installed. In the latter case, you"
          echo "*** may want to edit the gsl-config script: $GSL_CONFIG" ])
          CFLAGS="$ac_save_CFLAGS"
          LIBS="$ac_save_LIBS"
       fi
     fi
#     GSL_CFLAGS=""
#     GSL_LIBS=""
     ifelse([$3], , :, [$3])
  fi
  AC_SUBST(GSL_CFLAGS)
  AC_SUBST(GSL_LIBS)
  rm -f conf.gsltest
])

# pkg.m4 - Macros to locate and utilise pkg-config.            -*- Autoconf -*-
# 
# Copyright © 2004 Scott James Remnant <scott@netsplit.com>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

# PKG_PROG_PKG_CONFIG([MIN-VERSION])
# ----------------------------------
AC_DEFUN([PKG_PROG_PKG_CONFIG],
[m4_pattern_forbid([^_?PKG_[A-Z_]+$])
m4_pattern_allow([^PKG_CONFIG(_PATH)?$])
AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])dnl
if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
	AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
fi
if test -n "$PKG_CONFIG"; then
	_pkg_min_version=m4_ifval([$1], [$1], [0.9.0])
	AC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version])
	if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then
		AC_MSG_RESULT([yes])
	else
		AC_MSG_RESULT([no])
		PKG_CONFIG=""
	fi
		
fi[]dnl
])# PKG_PROG_PKG_CONFIG

# _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES])
# ---------------------------------------------
m4_define([_PKG_CONFIG],
[if test "x$ac_cv_env_[]$1[]_set" = "xset"; then
	pkg_cv_[]$1=$ac_cv_env_[]$1[]_value
elif test -n "$PKG_CONFIG"; then
	if AC_RUN_LOG([$PKG_CONFIG --exists "$3" >/dev/null 2>&1]); then
		pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null`
	else
		pkg_failed=yes
	fi
else
	pkg_failed=untried
fi[]dnl
])# _PKG_CONFIG

# PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
# [ACTION-IF-NOT-FOUND])
#
#
# Note that if there is a possibility the first call to
# PKG_CHECK_MODULES might not happen, you should be sure to include an
# explicit call to PKG_PROG_PKG_CONFIG in your configure.in
#
#
# --------------------------------------------------------------
AC_DEFUN([PKG_CHECK_MODULES],
[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
AC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl
AC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl

pkg_failed=no
AC_CACHE_CHECK([for $1][_CFLAGS], [pkg_cv_][$1][_CFLAGS],
	[_PKG_CONFIG([$1][_CFLAGS], [cflags], [$2])])
AC_CACHE_CHECK([for $1][_LIBS], [pkg_cv_][$1][_LIBS],
	[_PKG_CONFIG([$1][_LIBS], [libs], [$2])])

if test $pkg_failed = yes; then
	$1[]_PKG_ERRORS=`$PKG_CONFIG --errors-to-stdout --print-errors "$2"`
	# Put the nasty error message in config.log where it belongs
	echo "$$1[]_PKG_ERRORS" 1>&AS_MESSAGE_LOG_FD

	ifelse([$4], , [AC_MSG_ERROR(dnl
[Package requirements ($2) were not met.
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively you may set the $1_CFLAGS and $1_LIBS environment variables
to avoid the need to call pkg-config.  See the pkg-config man page for
more details.])],
		[$4])
elif test $pkg_failed = untried; then
	ifelse([$4], , [AC_MSG_FAILURE(dnl
[The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

Alternatively you may set the $1_CFLAGS and $1_LIBS environment variables
to avoid the need to call pkg-config.  See the pkg-config man page for
more details.

To get pkg-config, see <http://www.freedesktop.org/software/pkgconfig>.])],
		[$4])
else
	$1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS
	$1[]_LIBS=$pkg_cv_[]$1[]_LIBS
	ifelse([$3], , :, [$3])
fi[]dnl
])# PKG_CHECK_MODULES
